<?php

class IndexController extends MACController
{
	public function indexAction()
	{
		$this->logger->info("IndexController::indexAction");

		$userModel = UserModel::getInstance ( "user", "user_id" );
		$userTmp = $userModel->getUserList();

		$this->render();
	}

	public function infosAction(){
		$this->logger->info("IndexController::infosAction");	
		$this->render();
	}
	
	public function contactAction()
	{
		$this->logger->info("IndexController::contactAction");
		$this->render();
	}

	public function loginAction()
	{
		$request = $this->getRequest();
		
		if(! $request->isPost()){return $this->_helper->redirector('index');}

		$dbAdapter = new Zend_Db_Adapter_Pdo_Mysql(array(
		    'host'     => $this->config->database->dbhost,
		    'username' => $this->config->database->dbusername,
		    'password' => $this->config->database->dbpassword,
		    'dbname'   => $this->config->database->dbname
		));

		$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter,'user','user_name','user_password');

		
		//set login form values
		$submitUsername = $request->getParam("username");
		$submitPassword = $request->getParam("password");
		
		$this->logger->info("IndexController::loginAction > username = $submitUsername password = $submitPassword");
		
		$authAdapter->setIdentity($submitUsername)->setCredential($submitPassword);
 
		// Réalise la requête d'authentification, et sauvegarde le résultat
		$result = $authAdapter->authenticate();
		$auth = Zend_Auth::getInstance();	
		
		if ($result->isValid()) {
			$user = $authAdapter->getResultRowObject();
			$auth->getStorage()->write($user);
			Zend_Registry::set('user',$user);
			$session = new Zend_Session_Namespace();
			$session->user = $user;
			
			$this->logger->info("Good identity !");
			$this->logger->info(print_r($user,true));
			$this->renderScript("index/login_redirect.phtml");	
			
			$this->getResponse()->setRawHeader("Refresh: 5;URL=/home");
		} else {
			$this->logger->info("Invalid indentity !");
			$this->renderScript("index/login_error.phtml");

		}
	}
	
	public function logoutAction()
	{
		$auth = Zend_Auth::getInstance();
		$auth->clearIdentity();
		$this->logger->info("Logout !");
		$this->getResponse()->setRawHeader("Refresh: 5;URL=/home");
	}
}

